﻿using Lab1.Controler;
using Lab1.DataObjects;
using Lab1.Utils;
using Microsoft.Win32;
using PorterStemmerAlgorithm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Utils;

namespace Lab1.ViewModels
{
    internal class MainWindowViewModel : INotifyPropertyChanged
    {
        public MainWindowViewModel()
        {
            _controler = new MainWindowControler { _viewModel = this };
        }

        public ExpandQuerry eq = null;

        private MainWindowControler _controler {get; set;}

        public event PropertyChangedEventHandler PropertyChanged;

        private String _expandQuerryResult { get; set; }
        public String ExpandQuerryResult
        {
            get
            {
                return _expandQuerryResult;
            }
            set
            {
                _expandQuerryResult = value;                
                OnPropertyChanged("ExpandQuerryResult");
            }
        }

        private bool _expandQuerry { get; set; }
        public bool ExpandQuerry
        {
            get
            {
                return _expandQuerry;
            }
            set
            {
                _expandQuerry = value;
                if (_expandQuerry)
                {
                    eq = new ExpandQuerry(5);

                }
            }
        }

        private bool _showStemmed { get; set; }
        public bool ShowStemmed
        {
            get
            {
                return _showStemmed;
            }
            set
            {
                _showStemmed = value;
                if (ShowStemmed)
                {
                    if (_selectedDocument != null)
                    {
                        SelectedContent = _selectedDocument.ContentStemmed;
                        SelectedTitle = _selectedDocument.TitleStemmed;
                    }
                    if (TermsColl != null)
                    {
                        Terms = String.Join(", ", (from t in _termsColl select t.KeywordStemmed).ToArray());
                    }
                }
                else
                {
                    if (_selectedDocument != null)
                    {
                        SelectedContent = _selectedDocument.Content;
                        SelectedTitle = _selectedDocument.Title;
                    }
                    if (TermsColl != null)
                    {
                        Terms = String.Join(", ", (from t in _termsColl select t.Keyword).ToArray());
                    }
                }
            }
        }
        private Document _selectedDocumentRanking { get; set; }
        public Document SelectedDocumentRanking
        {
            get
            {
                return _selectedDocumentRanking;
            }
            set
            {
                _selectedDocumentRanking = value;
                if (_selectedDocumentRanking != null)
                {
                    RankingSelectedContent = _selectedDocumentRanking.Content;
                    RankingSelectedTitle = _selectedDocumentRanking.Title;
                    SelectedScore = _selectedDocumentRanking.Score.ToString();
                }
                
            }
        }

        private string _selectedScore { get; set; }
        public string SelectedScore
        {
            get
            {
                return _selectedScore;
            }
            set
            {
                if (value != _selectedScore)
                {
                    _selectedScore = value;
                    OnPropertyChanged("SelectedScore");
                }
            }
        }

        private string _rankingSelectedContent { get; set; }
        public string RankingSelectedContent
        {
            get
            {
                return _rankingSelectedContent;
            }
            set
            {
                if (value != _rankingSelectedContent)
                {
                    _rankingSelectedContent = value;
                    OnPropertyChanged("RankingSelectedContent");
                }
            }
        }

        private string _rankingSelectedTitle { get; set; }
        public string RankingSelectedTitle
        {
            get
            {
                return _rankingSelectedTitle;
            }
            set
            {
                if (value != _rankingSelectedTitle)
                {
                    _rankingSelectedTitle = value;
                    OnPropertyChanged("RankingSelectedTitle");
                }
            }
        }

        private Document _selectedDocument { get; set; }
        public Document SelectedDocument
        {
            get
            {
                return _selectedDocument;
            }
            set
            {
                _selectedDocument = value;
                if (ShowStemmed)
                {
                    if (_selectedDocument != null)
                    {
                        SelectedContent = _selectedDocument.ContentStemmed;
                        SelectedTitle = _selectedDocument.TitleStemmed;
                    }
                    if (TermsColl != null)
                    {
                        Terms = String.Join(", ", (from t in _termsColl select t.KeywordStemmed).ToArray());
                    }
                }
                else
                {
                    if (_selectedDocument != null)
                    {
                        SelectedContent = _selectedDocument.Content;
                        SelectedTitle = _selectedDocument.Title;
                    }
                    if (TermsColl != null)
                    {
                        Terms = String.Join(", ", (from t in _termsColl select t.Keyword).ToArray());
                    }
                }
            }
        }

        private string _selectedContent { get; set; }
        public string SelectedContent
        {
            get
            {
                return _selectedContent;
            }
            set
            {
                if (value != _selectedContent)
                {
                    _selectedContent = value;
                    OnPropertyChanged("SelectedContent");
                }
            }
        }

        private string _selectedTitle { get; set; }
        public string SelectedTitle
        {
            get
            {
                return _selectedTitle;
            }
            set
            {
                if (value != _selectedTitle)
                {
                    _selectedTitle = value;
                    OnPropertyChanged("SelectedTitle");
                }
            }
        }

        private string _terms { get; set; }
        public string Terms
        {
            get
            {
                return _terms;
            }
            set
            {
                if (value != _terms)
                {
                    _terms = value;
                    OnPropertyChanged("Terms");
                }
            }
        }

        private string _query { get; set; }
        public string Query
        {
            get
            {
                return _query;
            }
            set
            {
                if (value != _query)
                {
                    _query = value;
                    OnPropertyChanged("Query");
                }
            }
        }

        private ICommand _openFileCommand;
        public ICommand OpenFileCommand
        {
            get
            {
                if(_openFileCommand == null)
                    _openFileCommand = new DelegateCommand(_controler.OpenFile);
                return _openFileCommand;
            }
        }

        private ICommand _openTermsFileCommand;
        public ICommand OpenTermsFileCommand
        {
            get
            {
                if (_openTermsFileCommand == null)
                    _openTermsFileCommand = new DelegateCommand(_controler.OpenTermsFile);
                return _openTermsFileCommand;
            }
        }

        private ICommand _queryCommand;
        public ICommand QueryCommand
        {
            get
            {
                if (_queryCommand == null)
                    _queryCommand = new DelegateCommand(_controler.ProcessQuery);
                return _queryCommand;
            }
        }

        private ICommand _groupCommand;
        public ICommand GroupCommand
        {
            get
            {
                if (_groupCommand == null)
                    _groupCommand = new DelegateCommand(_controler.GroupDocuments);
                return _groupCommand;
            }
        }

        private ObservableCollection<Document> _documents;
        public ObservableCollection<Document> Documents
        {
            get
            {
                return _documents;
            }
            set
            {
                if (value != _documents)
                {
                    _documents = value;
                    OnPropertyChanged("Documents");
                }
            }
        }

        private ObservableCollection<Document> _documentsRanking;
        public ObservableCollection<Document> DocumentsRanking
        {
            get
            {
                return _documentsRanking;
            }
            set
            {
                if (value != _documentsRanking)
                {
                    _documentsRanking = value;
                    SelectedDocumentRanking = _documentsRanking.FirstOrDefault();
                    OnPropertyChanged("DocumentsRanking");
                }
            }
        }

        private List<Term> _termsColl;
        public List<Term> TermsColl
        {
            get
            {
                return _termsColl;
            }
            set
            {
                if (value != _termsColl)
                {
                    _termsColl = value;
                    if (ShowStemmed)
                    {
                        Terms = String.Join(", ",( from t in _termsColl select t.KeywordStemmed).ToArray());
                    }
                    else
                    {
                        Terms = String.Join(", ", (from t in _termsColl select t.Keyword).ToArray());
                    }
                    OnPropertyChanged("TermsColl");
                }
            }
        }

        
        protected void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, e);
        }

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        private int _groupCount { get; set; }
        public int GroupCount
        {
            get
            {
                return _groupCount;
            }
            set
            {
                _groupCount = value;
                OnPropertyChanged("GroupCount");
            }
        }

        private int _maxIteration { get; set; }
        public int MaxIteration
        {
            get
            {
                return _maxIteration;
            }
            set
            {
                _maxIteration = value;
                OnPropertyChanged("MaxIteration");
            }
        }

        public ObservableCollection<List<Document>> _groups { get; set; }
        public ObservableCollection<List<Document>> Groups
        {
            get
            {
                return _groups;
            }
            set
            {
                _groups = value;
                OnPropertyChanged("Groups");
            }
        }
        
    }
}
